package Interface;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

import controleur.ControleurRecu;
import controleur.ControleurVisualisationFacture;

@SuppressWarnings("serial")
public class PayerFacture extends JFrame implements Observer {
	private FacturerSejour m_facturerSejour;
	private Reservation  m_reservation;
	private ControleurVisualisationFacture m_controleur;
	private ControleurRecu m_controleurRecu;
	private JTextField txtSousTotal;
	private JTextField txtTPS;
	private JTextField txtTVQ;
	private JTextField txtTotal;

	/**
	 * Create the application.
	 */
	public PayerFacture(Reservation pReservation,FacturerSejour pFacturerSejour, ControleurVisualisationFacture pControleur, int[] idChambres) {
		
		initialize();	
		m_facturerSejour = pFacturerSejour;
		m_controleur = pControleur;		
		m_controleurRecu = new ControleurRecu(m_controleur.getFacture(), idChambres);
		m_controleurRecu.addObserver(this);
		m_reservation = pReservation;
		m_controleur.addObserver(this);
		m_controleur.getData();
	
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		this.setBounds(100, 100, 240, 200);
		setResizable(false);
		getContentPane().setLayout(null);
		
		JLabel lblSousTotal = new JLabel("Sous-total :");
		lblSousTotal.setBounds(10, 11, 64, 14);
		this.getContentPane().add(lblSousTotal);
		
		JLabel lblTPS = new JLabel("TPS :");
		lblTPS.setBounds(10, 42, 32, 14);
		this.getContentPane().add(lblTPS);
		
		JLabel lblTVQ = new JLabel("TVQ :");
		lblTVQ.setBounds(10, 73, 46, 14);
		this.getContentPane().add(lblTVQ);
		
		JLabel lblTotal = new JLabel("Total :");
		lblTotal.setBounds(10, 104, 46, 14);
		this.getContentPane().add(lblTotal);
		
		txtSousTotal = new JTextField();
		txtSousTotal.setEditable(false);
		txtSousTotal.setBounds(121, 8, 86, 20);
		getContentPane().add(txtSousTotal);
		txtSousTotal.setColumns(10);
		
		txtTPS = new JTextField();
		txtTPS.setEditable(false);
		txtTPS.setColumns(10);
		txtTPS.setBounds(121, 39, 86, 20);
		getContentPane().add(txtTPS);
		
		txtTVQ = new JTextField();
		txtTVQ.setEditable(false);
		txtTVQ.setColumns(10);
		txtTVQ.setBounds(121, 70, 86, 20);
		getContentPane().add(txtTVQ);
		
		txtTotal = new JTextField();
		txtTotal.setEditable(false);
		txtTotal.setColumns(10);
		txtTotal.setBounds(121, 101, 86, 20);
		getContentPane().add(txtTotal);
		
		JButton btnPayer = new JButton("Payer");
		btnPayer.setBounds(121, 140, 86, 23);
		btnPayer.addActionListener(new PayerFactureListener(this));
		getContentPane().add(btnPayer);
		
		JButton btnImprimer = new JButton("Imprimer");
		btnImprimer.setBounds(10, 140, 101, 23);
		btnImprimer.addActionListener(new ImprimerFactureListener());
		getContentPane().add(btnImprimer);
		
		this.addWindowListener(new WindowClosing(this));
	}
	class ImprimerFactureListener implements ActionListener{
		@Override
		public void actionPerformed(ActionEvent e) {;
			m_controleurRecu.getTextRecu();
		}
	}
	class PayerFactureListener implements ActionListener{
		PayerFacture m_payerFacture;
		public PayerFactureListener(PayerFacture payerFacture)
		{
			m_payerFacture = payerFacture;
		}
		@Override
		public void actionPerformed(ActionEvent e) {
			m_controleur.payerFacture();
			m_reservation.setVisible(true);
			m_facturerSejour.setVisible(false);
			m_payerFacture.setVisible(false);
			
		}
	}
	
	class WindowClosing implements WindowListener {
		Observer m_observer;
		public WindowClosing(Observer o)
		{
			m_observer = o;
		}
		@Override
		public void windowActivated(WindowEvent arg0) {
		}

		@Override
		public void windowClosed(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowClosing(WindowEvent arg0) {
			m_controleur.deleteObserver(m_observer);
			m_facturerSejour.setVisible(true);	   
		}

		@Override
		public void windowDeactivated(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowDeiconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowIconified(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void windowOpened(WindowEvent arg0) {
			// TODO Auto-generated method stub
			
		}

	}
	
	@Override
	public void update(Observable o, Object arg) {
		if (arg != null && arg instanceof String) {
			javax.swing.JOptionPane.showMessageDialog(null, arg.toString());
		}
		txtSousTotal.setText(m_controleur.getSousTotal());
		txtTPS.setText(m_controleur.getTPS());
		txtTVQ.setText(m_controleur.getTVQ());
		txtTotal.setText(m_controleur.getTotal());
	}	
}
